package javaclientcp3110.View;

import java.awt.event.ActionEvent;
import javaclientcp3110.View.Component.CompAddView;
import javaclientcp3110.View.Component.CompEditView;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import javaclientcp3110.View.Component.CompDeleteView;
import javaclientcp3110.View.Product.ProductAddView;
import javaclientcp3110.View.Product.ProductDeleteView;
import javaclientcp3110.View.Product.ProductEditView;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;

/**
 *
 * @author Richard Irons
 */
public class MainView extends javax.swing.JFrame {
    private static MainView instance;
    /** Creates new form NewJFrame */
    private MainView() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e) {

        }
        initComponents();
        initAL();
        setLocationRelativeTo(null);
        this.setTitle("Java Client");
    }
    public static MainView getInstance() {
        if (instance == null) {
            instance = new MainView();
        }
        return instance;
    }
    public void setMainView(JPanel panel){        
        contentPanel.removeAll();
        contentPanel.add(panel,null);
        repaint();
        validate();
        
        //call garbage collection to clean up previous views.
        System.gc();
    }
    public void errorMessage(String message) {
        JOptionPane.showMessageDialog(null,
            message,
            "Error",
            JOptionPane.ERROR_MESSAGE);
    }
    public void successMessage(String message) {
        JOptionPane.showMessageDialog(null,
            message,
            "Success!",
            JOptionPane.INFORMATION_MESSAGE);
    }
    private void initAL() {
        loginMenuItem.addActionListener(null);
        logoutMenuItem.addActionListener(null);

        exitMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        compAddMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setMainView(new CompAddView());
            }
        });
        compEditMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setMainView(new CompEditView());
            }
        });
        compDeleteMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setMainView(new CompDeleteView());
            }
        });
        prodAddMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setMainView(new ProductAddView());
            }
        });
        prodEditMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setMainView(new ProductEditView());
            }
        });
        prodDeleteMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setMainView(new ProductDeleteView());
            }
        });
    }

    //generated code with the netbeans gui builder
    private void initComponents() {

        contentPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        tilteMenuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        loginMenuItem = new javax.swing.JMenuItem();
        logoutMenuItem = new javax.swing.JMenuItem();
        exitMenuItem = new javax.swing.JMenuItem();
        componentMenu = new javax.swing.JMenu();
        compAddMenuItem = new javax.swing.JMenuItem();
        compEditMenuItem = new javax.swing.JMenuItem();
        compDeleteMenuItem = new javax.swing.JMenuItem();
        productMenu = new javax.swing.JMenu();
        prodAddMenuItem = new javax.swing.JMenuItem();
        prodEditMenuItem = new javax.swing.JMenuItem();
        prodDeleteMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(500, 400));
        setResizable(false);

        tilteMenuBar.setMaximumSize(new java.awt.Dimension(151, 20));
        tilteMenuBar.setMinimumSize(new java.awt.Dimension(0, 0));
        tilteMenuBar.setPreferredSize(new java.awt.Dimension(151, 20));

        fileMenu.setText("File");

        loginMenuItem.setText("Login");
        fileMenu.add(loginMenuItem);

        logoutMenuItem.setText("Logout");
        fileMenu.add(logoutMenuItem);

        exitMenuItem.setText("Exit");
        fileMenu.add(exitMenuItem);

        tilteMenuBar.add(fileMenu);

        componentMenu.setText("Component");

        compAddMenuItem.setText("Add");
        componentMenu.add(compAddMenuItem);

        compEditMenuItem.setText("Edit");
        componentMenu.add(compEditMenuItem);

        compDeleteMenuItem.setText("Delete");
        componentMenu.add(compDeleteMenuItem);

        tilteMenuBar.add(componentMenu);

        productMenu.setText("Product");

        prodAddMenuItem.setText("Add");
        productMenu.add(prodAddMenuItem);

        prodEditMenuItem.setText("Edit");
        productMenu.add(prodEditMenuItem);

        prodDeleteMenuItem.setText("Delete");
        productMenu.add(prodDeleteMenuItem);

        tilteMenuBar.add(productMenu);

        setJMenuBar(tilteMenuBar);

        BorderLayout layout = new BorderLayout();
        getContentPane().setLayout(layout);
        add(contentPanel,BorderLayout.CENTER);
    }

    // Variables declaration - do not modify
    private javax.swing.JMenuItem compAddMenuItem;
    private javax.swing.JMenuItem compDeleteMenuItem;
    private javax.swing.JMenuItem compEditMenuItem;
    private javax.swing.JMenu componentMenu;
    private javax.swing.JPanel contentPanel;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenuItem loginMenuItem;
    private javax.swing.JMenuItem logoutMenuItem;
    private javax.swing.JMenuItem prodAddMenuItem;
    private javax.swing.JMenuItem prodDeleteMenuItem;
    private javax.swing.JMenuItem prodEditMenuItem;
    private javax.swing.JMenu productMenu;
    private javax.swing.JMenuBar tilteMenuBar;
    // End of variables declaration
}
